package cn.iocoder.yudao.module.jikong.controller.admin.outUser;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.jikong.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.user.JkUserDO;
import cn.iocoder.yudao.module.jikong.service.user.JkUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 机构")
@RestController
@RequestMapping("/jikong/out-user")
@Validated
public class JkOutUserController {

    @Resource
    private JkUserService jkUserService;

//    @PostMapping("/create")
//    @Operation(summary = "创建机构")
//    @PreAuthorize("@ss.hasPermission('jikong:out-user:create')")
//    public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO createReqVO) {
//        return success(jkUserService.createUser(createReqVO));
//    }

    @PutMapping("/update")
    @Operation(summary = "更新机构")
    @PreAuthorize("@ss.hasPermission('jikong:out-user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserSaveReqVO updateReqVO) {
        jkUserService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机构")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:out-user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        jkUserService.deleteUser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机构")
                @PreAuthorize("@ss.hasPermission('jikong:out-user:delete')")
    public CommonResult<Boolean> deleteUserList(@RequestParam("ids") List<Long> ids) {
        jkUserService.deleteUserListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机构")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:out-user:query')")
    public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) {
        JkUserDO user = jkUserService.getUser(id);
        return success(BeanUtils.toBean(user, UserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机构分页")
    @PreAuthorize("@ss.hasPermission('jikong:out-user:query')")
    public CommonResult<PageResult<UserRespVO>> getUserPage(@Valid UserPageReqVO pageReqVO) {
        pageReqVO.setUserType(1);
        PageResult<JkUserDO> pageResult = jkUserService.getUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机构 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:out-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserExcel(@Valid UserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkUserDO> list = jkUserService.getUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机构.xls", "数据", UserRespVO.class,
                        BeanUtils.toBean(list, UserRespVO.class));
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "更新机构审核状态")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserStatusReqVO updateReqVO) {
        UserSaveReqVO userSaveReqVO = new UserSaveReqVO();
        BeanUtils.copyProperties(updateReqVO, userSaveReqVO);
        jkUserService.updateUser(userSaveReqVO);
        return success(true);
    }

    @PutMapping("/updateBackListStatus")
    @Operation(summary = "更新机构黑名单状态")
    public CommonResult<Boolean> updateBackListStatus(@Valid @RequestBody UserBackListStatusReqVO updateReqVO) {
        UserSaveReqVO userSaveReqVO = new UserSaveReqVO();
        BeanUtils.copyProperties(updateReqVO, userSaveReqVO);
        jkUserService.updateUser(userSaveReqVO);
        return success(true);
    }

}
